package com.qding.park.manager.service.core.facade.impl.common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.qding.park.common.core.exceptions.BaseException;
import com.qding.park.common.core.exceptions.ExceptionMessage;
import com.qding.park.common.core.exceptions.business.manager.service.ServiceCoreException;
import com.qding.park.common.utils.ListUtil;
import com.qding.park.manager.adapter.factory.AdapterParkProxyFactory;
import com.qding.park.manager.common.pojo.ParkDataResult;
import com.qding.park.manager.facade.core.service.common.CarCommonFacade;
import com.qding.park.manager.pojo.TbCar;
import com.qding.park.manager.service.core.biz.CarBiz;
import com.qding.park.manager.service.core.mapper.TbCarMapper;
import com.qding.park.manager.service.core.mapper.TbParkspaceMapper;
import com.qding.park.manager.service.core.mapper.TbUserInfoMapper;


@Service("carCommonFacade")
public class CarCommonFacadeImpl implements CarCommonFacade {

    private static final Logger _log = LoggerFactory.getLogger(CarCommonFacadeImpl.class);

    @Autowired
    private CarBiz carBiz;

    @Autowired
    private AdapterParkProxyFactory adapterParkProxyFactory;

    @Autowired
    private TbParkspaceMapper tbParkspaceMapper;

    @Autowired
    private TbUserInfoMapper tbUserInfoMapper;

    @Autowired
    private TbCarMapper tbCarMapper;

    @Override
    public ParkDataResult queryMyCarsByBjcode(String bjUserId) {

        List<TbCar> cars = carBiz.queryMyCarsByBjcode(bjUserId);

        if (ListUtil.isEmpty(cars)) {
            return ParkDataResult.createEmpty();
        }
        ParkDataResult parkDataResult = ParkDataResult.createListDate();
        List list = new ArrayList();
        for (TbCar car : cars) {
            
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("carNum", car.getCarNum());
            map.put("isMonthCard", car.getCardType());
            List parklist=new ArrayList();
            for(TbCar.ParkInfo  parkInfo:car.getParkList()){
                Map<String, Object> parkInfoTemp = new HashMap<String, Object>();
                parkInfoTemp.put("parkId", parkInfo.getParkID());
                parkInfoTemp.put("parkName", parkInfo.getParkName());
                parkInfoTemp.put("cardExpiration", parkInfo.getCardExpiration()==null?0:parkInfo.getCardExpiration().getTime()/1000);
                parklist.add(parkInfoTemp);
            }
            map.put("parkList", parklist);
            list.add(map);
        }

        parkDataResult.putDataOnList(list);

        return parkDataResult;
    }

    /*
     * (非 Javadoc) Description:
     * 
     * @see
     * com.qding.park.manager.facade.core.service.CarFacade#queryCarInfoByCarNum
     * (java.lang.String)
     * 
     */
    @Override
    public ParkDataResult queryCarInfoByCarNum(String carNum) throws ServiceCoreException {
        List<TbCar> cars = carBiz.queryCarOfParkByCarNum(carNum);

        if (ListUtil.isEmpty(cars)) {
            // throw ParkManagerCoreServerException.NON_CAR_EXCEPTION;
            throw new ServiceCoreException(ServiceCoreException.TypeEnum.EXCEPTION_NOFIND_CAR.getId(), null,
                    ExceptionMessage.createExceptionMessage(
                            ServiceCoreException.TypeEnum.EXCEPTION_NOFIND_CAR.getDesc(), "", "carNum=" + carNum));
        }

        ParkDataResult parkDataResult = ParkDataResult.createListDate();
        List list = new ArrayList();
        for (TbCar car : cars) {

            Map<String, Object> map = new HashMap<String, Object>();
            map.put("parkId", car.getParkID());
            map.put("parkName", car.getParkName());
            map.put("carNum", car.getCarNum());
            map.put("isMonthCard", car.getCardType());
            map.put("cardExpiration", car.getCardExpiration() == null ? 0 : car.getCardExpiration().getTime() / 1000);
            list.add(map);
        }

        parkDataResult.putDataOnList(list);

        return parkDataResult;
    }


    @Override
    public ParkDataResult queryCarInfo(String bjuserid, String carNum) throws ServiceCoreException {

        List<TbCar> cars = carBiz.queryCarInfo(bjuserid, carNum);

        if (ListUtil.isEmpty(cars)) {
            throw new ServiceCoreException(ServiceCoreException.TypeEnum.EXCEPTION_NOFIND_CAR.getId(), null,
                    ExceptionMessage.createExceptionMessage(
                            ServiceCoreException.TypeEnum.EXCEPTION_NOFIND_CAR.getDesc(), "", "carNum=" + carNum));
        }

        ParkDataResult parkDataResult = ParkDataResult.createListDate();
        List list = new ArrayList();
        for (TbCar car : cars) {

            Map<String, Object> map = new HashMap<String, Object>();
            map.put("parkId", car.getParkID());
            map.put("parkName", car.getParkName());
            map.put("carNum", car.getCarNum());
            map.put("isMonthCard", car.getCardType());
            map.put("cardExpiration", car.getCardExpiration() == null ? 0 : car.getCardExpiration().getTime() / 1000);
            list.add(map);
        }

        parkDataResult.putDataOnList(list);

        return parkDataResult;
    }

    @Override
    public ParkDataResult mycars_wx(long phone, String userId) {
        List<TbCar> cars = carBiz.mycars_wx(phone, userId);

        if (ListUtil.isEmpty(cars)) {
            return ParkDataResult.createEmpty();
        }
        ParkDataResult parkDataResult = ParkDataResult.createListDate();
        List list = new ArrayList();
        for (TbCar car : cars) {

            Map<String, Object> map = new HashMap<String, Object>();
            map.put("carNum", car.getCarNum());
            // map.put("isMonthCard", car.getCardType());
            list.add(map);
        }

        parkDataResult.putDataOnList(list);

        return parkDataResult;
    }

    @Override
    public ParkDataResult addCar_wx(long phone, String bjuserId, String carNum) throws BaseException {

        carBiz.addCar_wx(phone, bjuserId, carNum);
        return ParkDataResult.createEmpty();
    }

    @Override
    public ParkDataResult deleteCar_wx(long phone, String userId, String carNum) {
        carBiz.deleteCar_wx(phone, userId, carNum);
        return ParkDataResult.createEmpty();
    }

}
